package com.project8.main;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile; 
import org.apache.http.util.ByteArrayBuffer;
import com.project8.book.Book;
import com.project8.book.Chapter;
import com.project8.book.Page;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;


public class BookGenerator extends AsyncTask<Void, Void, Void> {
	  	
		 private String bookName;
		 private int id;
		 private Context context;
		 private String[] chapters = null;
		 private String[] chapterPages = null;
		 private ProgressDialog progress;
		 
		 //Books
		 private ArrayList<Book> books = Global.books;
		
		 //Internal Storage
		 private static final String internalStorage= Environment.getExternalStorageDirectory() + "/Textbooks/";
		

		 public BookGenerator(ProgressDialog P, Context context, String bookName, int id) {
			this.progress = P;
			this.context = context;
		    this.bookName = bookName;
		    this.id = id;
		  }

		  public void onPreExecute() {
			System.out.println("Here");  
			progress.setMessage("Generating Book");
			progress.show();
			
		  }

		  public Void doInBackground(Void... unused) 
		  {			
			  ArrayList<Chapter> chaptersInBook = new ArrayList<Chapter>();
			
			  File fChapters = new File(internalStorage + bookName + "/Chapters");
			  chapters = fChapters.list();
						
			  reverseArray(chapters);
			  			  			
			  Chapter.chapter_id = 0;
			  Page.numberOfPages = 1;
								
			  //For Each Chapter Create new Chapter in Book, Search Each Chapter for Pages
			  for(int j = 0; j < chapters.length; j++)
			  {
				  chaptersInBook.add(new Chapter(chapters[j]));
				
				  File fPages = new File(internalStorage + bookName + "/Chapters/" + chapters[j]);
				  chapterPages = fPages.list();
				
				  reverseArray(chapterPages);
				
				  //Create new Page in Book and Add to Chapter for Each Page
				  for(int k = 0; k < chapterPages.length; k++)
				  {
					  Page P = new Page(chapterPages[k], chaptersInBook.get(j));
					  P.setPageContent("file:///" + internalStorage + bookName + "/Chapters/" + chapters[j] + "/" + chapterPages[k]);
										
					  try 
					  {
						  FileInputStream pageIn = new FileInputStream(internalStorage + bookName + "/Chapters/" + chapters[j] + "/" + chapterPages[k]);
						
						  P.findImages(pageIn);
					  } 
					  catch (IOException e) 
					  {
						  e.printStackTrace();
					  }

					  chaptersInBook.get(j).addOnePage(P);
									
				  }
			  }
			
			  for(int f = 0; f < books.size(); f++)
			  {
				  if(books.get(f).getBookTitle().equals(bookName))
				  {
					  books.get(f).setChaptersInBook(chaptersInBook);
					  books.get(f).setGenerated(true);
				  }
			  }
			
			  return null;
		  }  

		  public void onPostExecute(Void unused) {
			progress.dismiss();
		    
			//Bundle Up Book
			Bundle bun = new Bundle();
			//bun.putSerializable("Book", books.get(v.getId()));
			bun.putInt("bookIndex", id);
			//Start Main App
			Intent myIntent = new Intent(context, Main.class);
        	myIntent.putExtras(bun);  	            	
            context.startActivity(myIntent);
		  }
		  		  
		  public void reverseArray(String[] backwards)
		  {
				for(int c = 0; c < backwards.length; c++)
				{
					int opposite = backwards.length-1 - c;
					
					if(opposite <= c)
						break;
					
					String temp = backwards[c];
					backwards[c] = backwards[opposite];
					backwards[opposite] = temp;
				}
		 }
}
		  
		        
       
               		
                	
                	



